<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

/**
 * 前台公共库文件
 * 主要定义前台公共函数库
 */

/**
 * 检测验证码
 * @param  integer $id 验证码ID
 * @return boolean     检测结果
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function check_verify($code, $id = 1){
	$verify = new \Think\Verify();
	return $verify->check($code, $id);
}

/**
 * 获取列表总行数
 * @param  string  $category 分类ID
 * @param  integer $status   数据状态
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_list_count($category, $status = 1){
    static $count;
    if(!isset($count[$category])){
        $count[$category] = D('Document')->listCount($category, $status);
    }
    return $count[$category];
}

/**
 * 获取段落总数
 * @param  string $id 文档ID
 * @return integer    段落总数
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_part_count($id){
    static $count;
    if(!isset($count[$id])){
        $count[$id] = D('Document')->partCount($id);
    }
    return $count[$id];
}

/**
 * 获取导航URL
 * @param  string $url 导航URL
 * @return string      解析或的url
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
function get_nav_url($url){
    switch ($url) {
        case 'http://' === substr($url, 0, 7):
        case '#' === substr($url, 0, 1):
            break;        
        default:
            $url = U($url);
            break;
    }
    return $url;
}


function int_to_string(&$data,$map=array('status'=>array(1=>'正常',-1=>'删除',0=>'禁用',2=>'未审核',3=>'草稿'))) {
    if($data === false || $data === null ){
        return $data;
    }
    $data = (array)$data;
    foreach ($data as $key => $row){
        foreach ($map as $col=>$pair){
            if(isset($row[$col]) && isset($pair[$row[$col]])){
                $data[$key][$col.'_text'] = $pair[$row[$col]];
            }
        }
    }
    return $data;
}

function comment_regx($content){
    $patter = "/@(\S*)[\s]+/";
    echo  preg_replace($patter ,"<span class='fc-height'>@$1 </span> ",$content);
}


/**
*与我相关评论的整合
*@author susy <susy.liang@ilongyuan.com.cn>
*/
function aggreateCmt($refCmt, $selfCmt){
    $comments = array();
    if($refCmt && $selfCmt){
        $comments = array_merge($refCmt, $selfCmt);
        $keyArr = array();
        foreach ($comments as $key => $value) {
            $keyArr[$key] = $value['unixtime'];
        }
        array_multisort($keyArr, SORT_DESC, $comments);
        $comments = array_chunk($comments, 5);
        $comments = $comments[0];
        /*foreach ($comments as $key => $value) {

            $comments[$key]['create_time'] = unixtime_format($value['create_time']);
        }*/
        return $comments;
    }elseif($refCmt){
        return $refCmt;
    }elseif($selfCmt){
        return $selfCmt;
    }else{
        return $comments;
    }
}

/**
 * 判断是否为手机端
 *@author susy <susy.liang@ilongyuan.com.cn>
 */
function is_mobile_request() {
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
        return true;
    
    //此条摘自TPM智能切换模板引擎，适合TPM开发
    if(isset ($_SERVER['HTTP_CLIENT']) &&'PhoneClient'==$_SERVER['HTTP_CLIENT'])
        return true;
    //如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset ($_SERVER['HTTP_VIA']))
        //找不到为flase,否则为true
        return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;
    //判断手机发送的客户端标志,兼容性有待提高
    if (isset ($_SERVER['HTTP_USER_AGENT'])) {
        $clientkeywords = array(
            'nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile'
        );
        //从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
            return true;
        }
    }
    //协议法，因为有可能不准确，放到最后判断
    if (isset ($_SERVER['HTTP_ACCEPT'])) {
        // 如果只支持wml并且不支持html那一定是移动设备
        // 如果支持wml和html但是wml在html之前则是移动设备
        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
            return true;
        }
    }
    return false;
 }

/**
*用户自动登录的cookie值的加解密
*@param string $strVal 操作的目标值
*@param int $type 0|加密 1|解密
*@return string 
*@author susy.liang<liangling@ilongyuan.com.cn>
*/
function check_auto_login($strVal, $type=0){

    $key =md5(C('AUTO_LOGIN_KEY'));

    if($type){
        $strVal = base64_decode($strVal);
        return $key^$strVal;
    }

   return str_replace('==', '', base64_encode($key^$strVal));

}

/**
*获取百分比
*@param int $firstNum 目标数
*@param int $secNum
*@return int
*/
function get_percent($firstNum=0, $secNum=0){
    $total = intval($firstNum + $secNum);
    if(!$total)
        return 100;
    $percent = intval($firstNum/$total*100);
    return $percent;
}

/** 
 * 判断是否来源微信
 * @author Legend. <xcx_legender@qq.com>
 * @param 
 * @return 
 */

function is_weixin(){
    return strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ;
}


function mobileBrower()
{
    $source = null;
    if(strpos($_SERVER["HTTP_USER_AGENT"],"MicroMessenger"))    //微信
    {
        $source = 'weixin';
    }
    else if(strpos($_SERVER["HTTP_USER_AGENT"],"Weibo"))    //微博
    {
        $source = 'sina';
    }
    else if(strpos($_SERVER["HTTP_USER_AGENT"],"QQBrowser"))    //微博
    {
        $source = 'qq';
    }
    else if (strpos($_SERVER["HTTP_USER_AGENT"],"QQ")){
        $source = 'qq';
    }
    // else{

    // }
    return $source;
}


